package com.finalab.yueqiu.service.user;

import com.finalab.yueqiu.constant.ResponseCode;
import com.finalab.yueqiu.dao.auth.UserAuthDAO;
import com.finalab.yueqiu.dao.auth.po.UserAuthPO;
import com.finalab.yueqiu.dao.redis.JedisDAO;
import com.finalab.yueqiu.dao.user.UserDAO;
import com.finalab.yueqiu.dao.user.po.UserPO;
import com.finalab.yueqiu.entity.user.User;
import com.finalab.yueqiu.exception.EntityAreadyExistsException;
import com.finalab.yueqiu.exception.EntityNotExistsException;
import com.finalab.yueqiu.exception.NotAuthException;
import com.finalab.yueqiu.exception.ParamIllegalException;
import com.finalab.yueqiu.util.EncryptionUtil;
import com.finalab.yueqiu.util.ResponseUtil;
import com.finalab.yueqiu.util.SaltUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 莉安
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    UserDAO userDAO;
    @Autowired
    JedisDAO jedisDAO;
    @Autowired
    UserAuthDAO userAuthDAO;

    private static final int HAVE_AUTH = 1;


    @Override
    public Map<String, Integer> register(User user){
        Integer code = ResponseCode.REQUEST_SUCCESSED.getValue();
        Integer id;
        HashMap<String, Integer> map = new HashMap(2);

        UserPO userPO1 = userDAO.selectByPhone(user.getPhone());
        if( userPO1 != null ){
            code = ResponseCode.ENTITY_ALREADY_EXISTS.getValue();
            map.put("code", code);
            return map;
        }else {
            UserAuthPO userAuthPO = userAuthDAO.selectByPhone(user.getPhone());
            if (userAuthPO == null || userAuthPO.getPhone() != HAVE_AUTH) {
                code = ResponseCode.USER_NOT_AUTH.getValue();
                map.put("code", code);
                return map;
            }
            String salt = SaltUtil.getSalt();
            byte[] password = EncryptionUtil.getEncryptedBytes(user.getPassword() + salt);
            UserPO userPO = new UserPO();
            userPO.setPhone(user.getPhone());
            userPO.setPassword(password);
            userPO.setSalt(salt);
            userDAO.insert(userPO);
            id = userDAO.lastInsertId();

            map.put("code", code);
            map.put("id", id);
        }


    return map;

    }

    @Override
    public Map<String, Integer> login(User user){
        UserPO userPO = userDAO.selectByPhone(user.getPhone());
        Integer code = ResponseCode.REQUEST_SUCCESSED.getValue();
        HashMap<String, Integer> map = new HashMap<>(2);
        Integer id = null;
        if(userPO != null){
            id = userPO.getId();
            boolean flag = Arrays.equals(EncryptionUtil.getEncryptedBytes(user.getPassword()+ userPO.getSalt()), userPO.getPassword());
            if(!flag){
                code = ResponseCode.PARAM_ILLEGAL.getValue();
                map.put("code", code);
                return map;
            }
        }else {
            code = ResponseCode.ENTITY_NOT_EXISTS.getValue();
        }
        map.put("id", id);
        map.put("code", code);
        return map;
    }
}
